package ebcs.scan.controller.userCtr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

import ebcs.app.auto.transmodel.RegNeedInforReqModel;
import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsUserDetailInfo;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.BsUserNeedRel;
import ebcs.database.bean.RpUserReportBean;
import ebcs.database.bean.RpUserTaskNeedBean;
import ebcs.database.service.BsUserInforServiceImp;
import ebcs.database.service.BsUserNeedServiceImp;
import ebcs.database.service.impl.BsUserInforServiceImpl;
import ebcs.database.service.impl.BsUserNeedServiceImpl;
import ebcs.scan.common.utils.StringUtil;
import jplat.core.session.JSession;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.string.DateUtil;
import jplat.tools.string.JRandomUtil;
 

/***
 * 用户需求报工处理
 * @author chenzhuo
 * @comment
 */
@Controller
public class UserNeedCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(UserNeedCtrl.class);
 

	protected JAppContextFactory serviceFactory = new JAppContextFactory();

	
	//数据库库服务.
	@Autowired 
	public BsUserInforServiceImp userInforService ; 
	@Autowired
	private BsUserNeedServiceImp userNeedService;
	
	public static String CHARSET_ENCODING = "UTF-8";
	 
	/**
	 * 微信登录
 	 */
	@RequestMapping("/page/sys/succ.do")
	public String succ( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		String succ = request.getParameter("succMsg");
		request.setAttribute("succMsg", "操作成功!");
		return "succ"; 
	}
	
	/**
	 * 客户端模块页面跳转
 	 */
	@RequestMapping("/page/login/{page}.do")
	public String doPage( HttpServletRequest request,HttpServletResponse response,@PathVariable String page ) throws JTransException, JSystemException{
		if("index".equals(page)){
 			JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
			JSession sess = context.getSession();
			BsUserInfor user = (BsUserInfor)sess.getUserInfo();
			RpUserTaskNeedBean condition = new RpUserTaskNeedBean();
			condition.setUserNo(user.getUserNo());
			List<RpUserTaskNeedBean> taskList = this.userNeedService.selectTaskNeedList(condition);
			request.setAttribute("taskList", taskList);
			request.setAttribute("taskSize", taskList.size());
			request.setAttribute("userName", user.getUserName());
		}
		return "frame/html/user/"+page; 
	}
	
	/**
	 * 客户端模块页面跳转
 	 */
	@RequestMapping("/page/common/{page}.do")
	public String doCommonPage( HttpServletRequest request,HttpServletResponse response,@PathVariable String page ) throws JTransException, JSystemException{
		return page; 
	}
	
	/**
	 * 待办列表查询
	 * @param request
	 * @param response
	 * @return
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/{needId}/doTaskHaving.do")
	public String doTaskHaving( HttpServletRequest request,HttpServletResponse response,@PathVariable String needId  ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		BsUserInfor user = context.getUserInfo();
 		List<BsNeedInfor> needList = this.userInforService.selectByNeedId(needId);
 		String sysDate = DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"); 
		request.setAttribute("sysDate", sysDate);
 		request.setAttribute("needBean", needList.get(0));
		request.setAttribute("userBean", user);
		return "frame/html/user/regTask"; 
	}
	
	
	
	
	
	 
	/**
	 * 查询历史已报工工时信息
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/{needId}/searchHistoryInfor.do")
	public String searchHistoryInfor( HttpServletRequest request,HttpServletResponse response,@PathVariable String needId  ) throws JTransException, JSystemException{
 		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		BsUserInfor userBean = context.getUserInfo();
		RpUserReportBean record = new  RpUserReportBean();
		record.setUserNo(userBean.getUserNo());
		record.setRouid(needId);
		List<RpUserReportBean> rsList = userNeedService.searchUserHistoryNeedInfor(record);
 		request.setAttribute("list", rsList);
 		if(rsList.size()>0){
 			request.setAttribute("needInfor",rsList.get(0));
 		}
 		request.setAttribute("size", rsList.size());
		return "frame/html/user/historyTask"; 
	}
	/**
	 * 删除历史报工数据
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/{rouid}/deleteUserNeedInfor.do")
	public void deleteUserNeedInfor( HttpServletRequest request,HttpServletResponse response,@PathVariable String rouid  ) throws JTransException, JSystemException{
 		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		BsUserInfor userBean = context.getUserInfo();
		int i = this.userNeedService.deleteUserNeedInfor(rouid);
		if(i==0){
			throw new JTransException("1001","删除数据失败!");
		}
		
		context.buildAppResponse(null);
		  
	}
	
	
	
	/**
	 * 登记需求工时情况
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/regNeedInfor.do")
	public void regNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
 		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		RegNeedInforReqModel req = context.obtainReqModel(RegNeedInforReqModel.class);
 		BsUserInfor userBean = context.getUserInfo();
 		//1、判断日期格式和工时是否合法。
 		if(!org.apache.commons.lang3.StringUtils.isNumeric(req.getNeedUseTime())){
 			throw new  JTransException("1001","你又乱填数字了哦，工时必须是数字！");
  		}
 		if(Double.parseDouble(req.getNeedUseTime())>24 || Double.parseDouble(req.getNeedUseTime())<=0){
 			throw new  JTransException("1001","你又乱填数字了哦，工时 >24h or <=0！");
 		}
 		//2、判断报工当天用户所有报工之和不能超过24小时。
 		RpUserTaskNeedBean condition = new RpUserTaskNeedBean();
 		condition.setUserNo(req.getUserNo());
 		condition.setHappenDate(req.getHappenDate());
 		int taskTime = this.userNeedService.selectUserTaskTime(condition);
 		if(taskTime>24){
 		   throw new  JTransException("1001","你的需求编号为:"+req.getNeedNo()+"在"+req.getHappenDate()+"累计报工已超过24小时了哦！");
 		}
 		//3、提醒当天的该需求已经报工，提醒是否覆盖。 
 		
 		//4、
 		BsUserNeedRel record = new BsUserNeedRel();
 		record.setRouid(JRandomUtil.getUUID());
 		record.setCreateDate(DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"));
 		record.setCreateTime(DateUtil.getSystemTimeBySomeFormat("hh:mm:ss"));
 		record.setNeedId(req.getNeedId());
 		record.setHappenDate(req.getHappenDate());
 		record.setNeedNo(req.getNeedNo());
 		record.setNeedUseTime(req.getNeedUseTime());
 		record.setRemark(req.getRemark());
 		record.setStage(req.getStage());
 		record.setUserNo(req.getUserNo());
 		record.setUserId(userBean.getRouid());
 		record.setReportState("00");
 		userNeedService.regUserNeedRel(record);
 		// 更新BS_NEED_INFOR的update_date
 		BsNeedInfor needInfor = new BsNeedInfor();
 		needInfor.setRouid(req.getNeedId());
 		userNeedService.updateNeedInfor(needInfor);
 		
 		Map rspModel = new HashMap();
		context.buildAppResponse(rspModel);
	}
	
	
	/**
	 * 查询其他项目组需求
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/searchTaskByTeam.do")
	public void searchTaskByTeam( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
 		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		BsUserInfor user = context.getUserInfo();
 		RpUserTaskNeedBean condition = new RpUserTaskNeedBean();
 		condition.setProTeamNo(user.getProTeamNo());
 		List<RpUserTaskNeedBean> taskList = this.userNeedService.selectTaskNeedListByTeamNo(condition);
 		Map rspModel = new HashMap<>();
 		rspModel.put("taskList", taskList);
 		
 		
 		context.buildAppResponse(rspModel);
	}
	
	/**
	 * 关闭需求
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/closeNeedInfor.do")
	public void closeNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		 JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
		 BsNeedInfor req = context.obtainReqModel(BsNeedInfor.class);
		 int i= userNeedService.updateStateByRouid(req.getRouid(),"02");
	     if(i==0)
	 	    	 throw new  JTransException("1001","需求关闭失败！");
	     Map rspModel = new HashMap();
	     context.buildAppResponse(rspModel);
	}
	/**
	 * 重新打开已关闭的需求
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/base/reStartNeedInfor.do")
	public void reStartNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		 JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
		 BsNeedInfor req = context.obtainReqModel(BsNeedInfor.class);
		 int i =userNeedService.updateStateByRouid(req.getRouid(),"01");
	     if(i==0)
	    	 throw new  JTransException("1001","需求重新打开失败！");
	     Map rspModel = new HashMap();
	     context.buildAppResponse(rspModel);
	}
	
}
